angular.module('mamApp').controller('specialTagsManageController', ['$scope', '$state', '$uibModal', 'ImsDialog', 'TagsManager', '$filter',
    function ($scope, $state, $uibModal, ImsDialog, TagsManager, $filter) {

        $scope.search = {
            dimensionId: $state.params.dimensionId,
            specialName: $state.params.specialName,
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10
        };

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        };


        TagsManager.specialDimensionsDetail($scope.search.dimensionId).then(function (result) {
            $scope.specialDimension = result.data.sdimensionVO;
            //console.log(result)
        });

        //标签列表
        TagsManager.sdimensionTags($scope.search.dimensionId, [$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            $scope.sdimensionTags = result.data.list;
            $scope.paginationConf.totalItems = result.data.total;
            console.log(result)
        });

        //搜索
        $scope.specialName = $scope.search.specialName;


        $scope.searchClick = function (specialName) {
            $scope.search.specialName = specialName;
            $state.go($state.current.name, $scope.search)
        };

        //初始化全选值为false
        $scope.checkData = {allChecked: false};
        //全选
        $scope.selectAllChange = function (checked) {
            $scope.sdimensionTags.forEach(function (item) {
                item.checked = checked;
            })
        };
        //选择一个
        $scope.selectOneChange = function (checked) {
            if (!checked) {
                $scope.checkData.allChecked = false;
            } else {
                ($filter('filter')($scope.sdimensionTags, {checked: true}).length) == $scope.sdimensionTags.length ? $scope.checkData.allChecked = true : $scope.checkData.allChecked = false;
            }
        };

        //新建数据
        $scope.create = function () {
            $scope.formData = {};
            $scope.formData.status = '2';
            $scope.formData.dimensionId = $scope.specialDimension.dimensionId;
            var modalInstance = $uibModal.open({
                animation: true,
                size: 'normal',
                openedClass: "modal-open-normal",
                templateUrl: 'specialTagsModal.html',
                controller: 'specialTagsModalController',
                resolve: {
                    data: function () {
                        return {formData:$scope.formData,specialDimension:$scope.specialDimension};
                    }
                }
            });
            modalInstance.result.then(function (result) {
                if (result == "ok") {
                    $state.reload();
                }
/*                if (result == "error") {
                    ImsDialog.open({title: "专用标签管理", message: "标签名称已存在。"}).then(function (a) {
                    })
                }*/
            })
        };

        //编辑数据
        $scope.edit = function (item) {
            $scope.formData = {};
            $scope.formData.specialId = item.specialId;
            $scope.formData.dimensionId = $scope.specialDimension.dimensionId;
            $scope.formData.specialName = item.specialName;
            $scope.formData.status = item.status;
            var modalInstance = $uibModal.open({
                animation: true,
                size: 'normal',
                openedClass: "modal-open-normal",
                templateUrl: 'specialTagsModal.html',
                controller: 'specialTagsModalController',
                resolve: {
                    data: function () {
                        return {formData:$scope.formData,specialDimension:$scope.specialDimension};
                    }
                }
            });
            modalInstance.result.then(function (result) {
                if (result == "ok") {
                    $state.reload();
                }
/*                if (result == "error") {
                    ImsDialog.open({title: "专用标签管理", message: "标签名称已存在。"}).then(function (a) {
                    })
                }*/
            })
        };

        var statusChange = {};

        //禁用一个
        $scope.disable = function (item) {
            statusChange.status = '2';
            ImsDialog.open({
                title: "专用标签管理",
                message: '确定禁用该标签？<br><small style="color: #CCCCCC">注：禁用后，将限制新专辑对禁用标签的使用，但不影响之前的专辑</small>'
            }).then(function (a) {
                if (a == "ok") {
                    TagsManager.specialTagStatusChange(item.specialId, statusChange).then(function (result) {
                       // console.log(result)
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        };

        //启用一个
        $scope.enable = function (item) {
            statusChange.status = '4';
            ImsDialog.open({title: "专用标签管理", message: '确定启用该标签？'}).then(function (a) {
                if (a == "ok") {
                    TagsManager.specialTagStatusChange(item.specialId, statusChange).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        };

        //禁用多个
        $scope.disableAll = function () {
            var tempArray = [];
            $scope.sdimensionTags.forEach(function (item) {
                if (item.checked == true) {
                    if (item.status == '4') {
                        tempArray.push(item.specialId)
                    }
                }
            });
            statusChange.status = '2';
            if (tempArray.length > 0) {
                ImsDialog.open({
                    title: "专用标签管理",
                    message: '确定禁用该标签？<br><small style="color: #CCCCCC">注：禁用后，将限制新专辑对禁用标签的使用，但不影响之前的专辑</small>'
                }).then(function (a) {
                    if (a == "ok") {
                        TagsManager.specialTagStatusChange(tempArray, statusChange).then(function (result) {
                            if (result.data.c == "s") {
                                $state.reload();
                            }
                        })
                    }
                })
            } else {
                return false;
                //ImsDialog.open({title: "专用标签管理", message: "没有可禁用的数据。"}).then(function (a) {})
            }
        };

        //启用多个
        $scope.enableAll = function () {
            var tempArray = [];
            $scope.sdimensionTags.forEach(function (item) {
                if (item.checked == true) {
                    if (item.status == '2') {
                        tempArray.push(item.specialId)
                    }
                }
            });
            statusChange.status = '4';
            if (tempArray.length > 0) {
                ImsDialog.open({title: "专用标签管理", message: '确定启用该标签？'}).then(function (a) {
                    if (a == "ok") {
                        TagsManager.specialTagStatusChange(tempArray, statusChange).then(function (result) {
                            if (result.data.c == "s") {
                                $state.reload();
                            }
                        })
                    }
                })
            } else {
                return false;
                //ImsDialog.open({title: "专用标签管理", message: "没有可启用的数据。"}).then(function (a) {})
            }
        };

        //删除一个数据
        $scope.delete = function (item) {
            ImsDialog.open({title: "专用标签管理", message: '确定删除该标签？'}).then(function (a) {
                if (a == "ok") {
                    TagsManager.specialTagDelete(item.specialId).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        };

        //删除多个数据
        $scope.deleteAll = function () {
            var tempArray = [];
            $scope.sdimensionTags.forEach(function (item) {
                if (item.checked == true) {
                    tempArray.push(item.specialId)
                }
            });
            if (tempArray.length > 0) {
                ImsDialog.open({title: "专用标签管理", message: '确定删除该标签？'}).then(function (a) {
                    if (a == "ok") {
                        TagsManager.specialTagDelete(tempArray).then(function (result) {
                            if (result.data.c == "s") {
                                $state.reload();
                            }
                        })
                    }
                })
            } else {
                return false;
                //ImsDialog.open({title: "专用标签管理", message: "没有可删除的数据。"}).then(function (a) {})
            }
        }



        $scope.keyup=function (e,specialName) {
            if(e.key=="Enter"){
                $scope.search.pageNo=1;
                $scope.search.specialName = specialName;
                $state.go($state.current.name, $scope.search)
            }
        }



    }]);

